package org.red5.demos.ofla;

import org.apache.commons.lang3.StringUtils;
import org.red5.demos.service.UserService;
import org.red5.server.api.IConnection;
import org.red5.server.api.Red5;
import org.red5.server.api.scope.IScope;
import org.red5.server.api.stream.IStreamPublishSecurity;
import org.red5.util.MD5Utils;
import org.red5.util.SpringContextHolder;


public class NamePublishSecurity implements IStreamPublishSecurity {
	 
	 
	private UserService userService;
	
	@Override
	public boolean isPublishAllowed(IScope scope, String name, String mode) {
		userService = SpringContextHolder.getBean("userService");
		System.out.println("userService:"+userService.list()); 
        //get the current connection 
        IConnection conn = Red5.getConnectionLocal();  
        //token to use for auth
        Object query = conn.getConnectParams().get("queryString");
        if(query == null){
        	return false;
        }
        String token = String.valueOf(query);
        if(!StringUtils.contains(token,"token=")){
        	return false;
        }
        String[] params = StringUtils.split(token,"=");
        if(params[1].equals(MD5Utils.encrypt(userService.getKey(),userService.getSecret()))){
        	System.out.println(params[1]);
        	System.out.println(userService.getKey());
        	System.out.println(userService.getSecret());
        	return true;
        }else{
        	return false;
        }
    	 
	}
}